package com.loyin.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;

import com.et.mvc.JsonView;
import com.et.mvc.View;
import com.loyin.model.MsgInfo;
import com.loyin.model.UserInfo;
import com.loyin.model.UserInfoVo;
import com.loyin.util.MD5;
import com.loyin.util.Page;

/***
 * 用户控制层
 * @author 刘声凤
 * 2011-11-5
 */
public class UserInfoController extends ApplicationController {
	private Logger log=Logger.getLogger(this.getClass());
	public void reg(){}
	public void home(){}
	
	public View save(UserInfo user){
		Map<String,Object> map=new HashMap<String,Object>();
		String checkcode=request.getParameter("checkcode");
		String ckcode=(String)session.getAttribute("check");
		if(checkcode==null||!checkcode.equals(ckcode)){
			map.put("stat", "F");
			map.put("msg", "验证码错误！");
			return new JsonView(map);
		}
		try{
			if(user.id==null||user.id==0){
				List<UserInfo> ulist=(List<UserInfo>)user.findBySql(user.getClass(), "select * from igs_userinfo where usercode=? or email =? or phone=? ", new Object[]{user.usercode,user.email,user.phone});
				if(ulist!=null&&ulist.isEmpty()==false){
					map.put("stat", "F");
					map.put("msg", "已经存在此会员号、手机号、email！");
					return new JsonView(map);
				}
				user.regtime=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
				user.setPwd(MD5.getMD5ofStr(user.getPwd()));
			}
			if(user.save()==true){
				map.put("stat", "S");
				map.put("msg", "注册会员信息成功！");
				map.put("arg",user.id);
			}else{
				map.put("stat", "F");
				map.put("msg", "注册会员信息失败！");
			}
		}catch(Exception e){
			log.error("注册异常",e);
			map.put("stat", "E");
			map.put("msg", "注册会员异常！");
		}
		return new JsonView(map);
	}
	public void upinfo(){
		UserInfoVo uservo=(UserInfoVo)session.getAttribute("user_session");
		UserInfo user=new UserInfo();
		try{
			user=user.find(user.getClass(), uservo.id);
			request.setAttribute("user",user);
		}catch(Exception e){
			log.error("",e);
		}
	}
	public View update(UserInfoVo uservo){
		Map<String,Object> map=new HashMap<String,Object>();
		try{
			UserInfo user=new UserInfo();
			user=user.find(user.getClass(), uservo.id);
			user.protectQestion=uservo.protectQestion;
			user.protectAnswer=uservo.protectAnswer;
			user.firstname=uservo.firstname;
			user.lastname=uservo.lastname;
			user.sex=uservo.sex;
			user.phone=uservo.phone;
			user.email=uservo.email;
			if(user.update()==true){
				map.put("stat", "S");
				map.put("msg", "修改会员信息成功！");
				map.put("arg",uservo.id);
			}else{
				map.put("stat", "F");
				map.put("msg", "修改会员信息失败！");
			}
		}catch(Exception e){
			log.error("修改异常",e);
			map.put("stat", "E");
			map.put("msg", "修改会员异常！");
		}
		return new JsonView(map);
	}
	public void pwd(){}
	/**
	 * 修改密码
	 * @param user
	 * @return
	 */
	public View updatepwd(){
		Map<String,Object> map=new HashMap<String,Object>();
		String oldpwd=request.getParameter("oldpwd");
		String newpwd=request.getParameter("newpwd");
		String pwd1=request.getParameter("pwd1");
		UserInfoVo uservo=(UserInfoVo)session.getAttribute("user_session");
		try{
			if(uservo==null){
				map.put("stat", "E");
				map.put("msg", "登录超时，请重新登录！");
				return new JsonView(map);
			}
			if(oldpwd==null||newpwd==null||pwd1==null||"".equals(pwd1)||"".equals(oldpwd)||"".equals(oldpwd)){
				map.put("stat", "F");
				map.put("msg", "未输入密码，请重新输入！");
			}else{
				if(MD5.getMD5ofStr(oldpwd.trim()).equals(uservo.getPwd())==false){
					map.put("stat", "F");
					map.put("msg", "原密码错误，请重新输入！");
				}else{
					UserInfo user=new UserInfo();
					newpwd=MD5.getMD5ofStr(newpwd);
					int rs=user.execute("update igs_userinfo u set u.pwd=? where u.id=?", new Object[]{newpwd,uservo.id});
					if(rs>0){
						map.put("stat", "S");
						map.put("msg", "修改密码成功！请牢记密码！");
						uservo.setPwd(newpwd);
					}else{
						map.put("stat", "F");
						map.put("msg", "修改密码失败！");
					}
				}
			}
		}catch(Exception e){
			log.error("修改密码失败",e);
			map.put("stat", "E");
			map.put("msg", "修改密码失败！");
		}
		return new JsonView(map);
	}
	@SuppressWarnings("unchecked")
	public View login(){
		Map<String,Object> map=new HashMap<String,Object>();
		String usercode=request.getParameter("usercode");
		String pwd=request.getParameter("pwd");
		String checkcode=request.getParameter("checkcode");
		String ckcode=(String)session.getAttribute("check");
		UserInfo user=new UserInfo();
		try{
			if(checkcode!=null&&checkcode.equals(ckcode)){
			 	
				List<UserInfo> userlist=(List<UserInfo>)user.findBySql(user.getClass(), "select ID,email,firstname,lastname,phone,protectAnswer,protectQestion,pwd,rechargeable,regtime,sex,spacesize,usercode from igs_userinfo where (usercode=? or email=? or phone=?) and pwd=? ",
			 												new Object[]{usercode,usercode,usercode,MD5.getMD5ofStr(pwd)});
			 	if(userlist==null||userlist.isEmpty()==true){
			 		map.put("stat", "F");
					map.put("msg", "用户名或密码错误！");
			 	}else{
			 		UserInfo userinfo=userlist.get(0);
			 		UserInfoVo uservo=new UserInfoVo();
			 		BeanUtils.copyProperties(uservo,userinfo);
			 		session.setAttribute("user_session", uservo);
			 		map.put("stat", "S");
					map.put("msg", "");
			 	}
			}else{
				map.put("stat", "F");
				map.put("msg", "验证码错误！");
			}
		}catch(Exception e){
			log.error("",e);
			map.put("stat", "F");
			map.put("msg", "登录异常！");
		}
		return new JsonView(map);
	}
	public View logout(){
		Map<String,Object> map=new HashMap<String,Object>();
		session.removeAttribute("user_session");
		map.put("stat", "S");
		return new JsonView(map);
	}
	public void loginform(){}
	public void list(){
		UserInfo UserInfo = new UserInfo();
		StringBuffer sql = new StringBuffer("select * from igs_UserInfo where 1=1 ");
		try {
			String pagenostr=request.getParameter("pageNo");
			String pagesizestr=request.getParameter("pageSize");
			if(pagenostr==null||"".equals(pagenostr.trim())){
				pagenostr="1";
			}
			if(pagesizestr==null||"".equals(pagesizestr.trim())){
				pagesizestr=this.pagesize+"";
			}
			Page page=new Page(0,Integer.parseInt(pagenostr),Integer.parseInt(pagesizestr),null);
			List<Object> paramlist=new ArrayList<Object>();
			
			List<UserInfo> list = (List<UserInfo>) UserInfo.findBySql(UserInfo.getClass(), sql.toString(), paramlist.toArray(),"",page.getPageSize(),page.start);
			long count=UserInfo.count(UserInfo.getClass(),null, paramlist.toArray());
			page=new Page(count,Integer.parseInt(pagenostr),Integer.parseInt(pagesizestr),list);
			request.setAttribute("page",page);
		} catch (Exception e) {
			log.error("",e);
			request.setAttribute("msg_error", "访问出错");
		}
	}
	public void showallinfo(){
		UserInfoVo uservo=(UserInfoVo)session.getAttribute("user_session");
		String sql=null;
		List<Object> params=new ArrayList<Object>();
		params.add(uservo.id);
		//通知信息
		try{
			sql="select count(*) from igs_msg where userid=? ";
			Object msgcount= MsgInfo.executeScalar(MsgInfo.class, sql,params.toArray());
			request.setAttribute("msgcount",msgcount);
		}catch(Exception e){
			log.error("通知信息",e);
		}
		//监控网址
		try{
			sql="select count(*) from igs_website where userid=? ";
			Object websitecount= MsgInfo.executeScalar(MsgInfo.class, sql,params.toArray());
			request.setAttribute("websitecount",websitecount);
		}catch(Exception e){
			log.error("监控网址",e);
		}
		//已捕获网址
		try{
			sql="select count(*) from igs_downloadurl where userid=? ";
			Object downurlcount= MsgInfo.executeScalar(MsgInfo.class, sql,params.toArray());
			request.setAttribute("downurlcount",downurlcount);
		}catch(Exception e){
			log.error("已捕获网址",e);
		}
	}
}
